package org.openkoala.koala.wizard.wizardpage.component;

import java.util.List;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.ITreeListAdapter;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.TreeListDialogField;
import org.eclipse.jface.layout.PixelConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.openkoala.koala.widget.Module;
import org.openkoala.koala.widget.Project;

/**
 * 模块信息管理界面
 * @author xmfang
 *
 */
@SuppressWarnings("restriction")
public class ModuleManage {
	
	private final int IDX_ADD= 0;
	private final int IDX_EDIT= 2;
	private final int IDX_REMOVE= 3;
	private final int IDX_DDD_MODULES = 6;

	private TreeListDialogField<Module> modulesViewList;

	private Control fSWTControl;
	
	private Project project;

	public List<Module> getModules() {
		return modulesViewList.getElements();
	}

	public ModuleManage(Project project) {
		this.project = project;
		fSWTControl= null;

		String[] buttonLabels= new String[] {
			"add",
			null,
			"edit",
			"remove",
			null,
			null,
			"User Default Modules"
		};

		ProjectsAdapter<Module> adapter= new ProjectsAdapter<Module>();
		modulesViewList = new TreeListDialogField<Module>(adapter, buttonLabels, new ModulesLabelProvider());
		modulesViewList.setLabelText("Project Modules");
		modulesViewList.enableButton(IDX_REMOVE, false);
		modulesViewList.enableButton(IDX_EDIT, false);
	}

	// -------- UI creation ---------
	public Control getControl(Composite parent) {
		PixelConverter converter = new PixelConverter(parent);

		Composite composite = new Composite(parent, SWT.NONE);

		LayoutUtil.doDefaultLayout(composite, new DialogField[] { modulesViewList }, true, SWT.DEFAULT, SWT.DEFAULT);
		//LayoutUtil.setHorizontalGrabbing(fProjectsList.getTreeControl(null));

		int buttonBarWidth = converter.convertWidthInCharsToPixels(24);
		modulesViewList.setButtonsMinWidth(buttonBarWidth);
		((GridData) modulesViewList.getTreeViewer().getControl().getLayoutData()).heightHint = converter.convertHeightInCharsToPixels(10);
		
		fSWTControl = composite;

		return composite;
	}

	/**
	 * 点击界面按钮是的处理
	 * @param field
	 * @param index
	 */
	private void modulePageCustomButtonPressed(DialogField field, int index) {
		if (index == IDX_ADD) {
			addModule();
			return;
		}
		if (index == IDX_EDIT) {
			editModule();
			return;
		}
		if (index == IDX_REMOVE) {
			removeModule();
			return;
		}
		if (index == IDX_DDD_MODULES) {
			generateDefaultModule();
			return;
		}
	}

	/**
	 * 添加模块
	 */
	private void addModule() {
		AddModuleDialog addModuleDialog = new AddModuleDialog(getShell(), SWT.SYSTEM_MODAL);
		addModuleDialog.setProject(project);
		addModuleDialog.open();

		Module module = addModuleDialog.getModule();
		if (module != null) {
			modulesViewList.addElement(module);
			project.getModule().add(module);
		}
	}

	/**
	 * 编辑模块
	 */
	private void editModule() {
		List selectedModules = modulesViewList.getSelectedElements();
		if (selectedModules.size() != 1) {
			return;
		}
		
		Module module = (Module) selectedModules.get(0);
		openEditModulePageToEdit(module);
	}

	/**
	 * 删除模块
	 */
	private void removeModule() {
		List selElements = modulesViewList.getSelectedElements();
		if (selElements.isEmpty()) {
			modulesViewList.refresh();
		} else {
			modulesViewList.removeElements(selElements);
			for (Object selElement : selElements) {
				Module selectedModule = (Module) selElement;
				project.removeModule(selectedModule);
			}
		}
	}

	/**
	 * 生成默认模块
	 */
	private void generateDefaultModule() {
		project.initSSJProject();
		project.initModulePrefix(project.getAppName());
		modulesViewList.removeAllElements();
		for (Module module : project.getModule()) {
			modulesViewList.addElement(module);
		}
	}
	
	/**
	 * @param field the dialog field
	 */
	private void projectPageDoubleClicked(TreeListDialogField<Module> field) {
		List selection = modulesViewList.getSelectedElements();
		Module module = (Module) selection.get(0);
		openEditModulePageToEdit(module);
	}

	private void openEditModulePageToEdit(Module module) {
		AddModuleDialog editModuleDialog = new AddModuleDialog(getShell(), SWT.SYSTEM_MODAL);
		editModuleDialog.setProject(project);
		editModuleDialog.setModule(module);
		editModuleDialog.setOperation("editModule");
		editModuleDialog.open();
		modulesViewList.update(module);
	}
	
	/**
	 * @param field the dialog field
	 */
	private void projectPageSelectionChanged(DialogField field) {
		modulesViewList.enableButton(IDX_EDIT, true);
		modulesViewList.enableButton(IDX_REMOVE, true);
		modulesViewList.enableButton(IDX_ADD, true);
	}

	private Shell getShell() {
		if (fSWTControl != null) {
			return fSWTControl.getShell();
		}
		return JavaPlugin.getActiveWorkbenchShell();
	}

    public void setFocus() {
    	modulesViewList.setFocus();
    }
    
	private class ProjectsAdapter<E> implements ITreeListAdapter<E> {

		public void customButtonPressed(TreeListDialogField<E> field, int index) {
			modulePageCustomButtonPressed(field, index);
		}

		public void selectionChanged(TreeListDialogField<E> field) {
			projectPageSelectionChanged(field);
		}

		public void doubleClicked(TreeListDialogField<E> field) {
			projectPageDoubleClicked((TreeListDialogField<Module>) field);
		}

		@Override
		public void keyPressed(TreeListDialogField<E> field, KeyEvent event) {
			//projectPageKeyPressed(field, event);
		}

		@Override
		public Object[] getChildren(TreeListDialogField<E> field, Object element) {
			return null;
		}

		@Override
		public Object getParent(TreeListDialogField<E> field, Object element) {
			return null;
		}

		@Override
		public boolean hasChildren(TreeListDialogField<E> field, Object element) {
			return false;
		}
	}
}
